﻿'Bram Debouvere
Option Strict On

Imports Gift.BL
Imports Gift.BL.GiftList


Public Class frmGiftLists



    Private Sub frmGiftLists_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillItems()
    End Sub

    Private Sub FillItems()
        'this function adds all giftlists to the filtered list view
        flvGeschenkenLijsten.Colums = {"Naam", "Datum Aanmaak", "Gelegenheid", "Persoon", "Code"}
        flvGeschenkenLijsten.Items.Clear()
        'grouping
        flvGeschenkenLijsten.lstView.Groups.Clear()
        'end grouping

        Dim list As List(Of GiftList) = GiftList.GetGiftListsFromDatabase(frmMain.dbh)
        For Each gList As GiftList In list
            If chkToonOudeLijsten.Checked Then
                Dim flvi As New FilteredListViewItem({gList.Naam, turnDateAround(gList.DatumAanmaak), gList.GelegenheidNaam, gList.PerNaam, gList.LijstCode}, gList)
                flvGeschenkenLijsten.Items.Add(flvi)
            Else
                If gList.LijstActief = True Then
                    Dim flvi As New FilteredListViewItem({gList.Naam, turnDateAround(gList.DatumAanmaak), gList.GelegenheidNaam, gList.PerNaam, gList.LijstCode}, gList)
                    flvGeschenkenLijsten.Items.Add(flvi)
                End If
            End If
        Next
        flvGeschenkenLijsten.FillListItems()
    End Sub

    Private Sub GroupedFillItems()
        'this function adds all giftlists to the filtered list view
        flvGeschenkenLijsten.Colums = {"Naam", "Datum Aanmaak", "Gelegenheid", "Persoon", "Code"}
        flvGeschenkenLijsten.Items.Clear()
        'grouping
        flvGeschenkenLijsten.lstView.Groups.Clear()
        'end grouping
        flvGeschenkenLijsten.FilterText = "" 'reset filtertext
        Dim list As List(Of GiftList) = GiftList.GetGiftListsFromDatabase(frmMain.dbh)
        For Each gList As GiftList In list
            If chkToonOudeLijsten.Checked Then
                Dim flvi As New FilteredListViewItem({gList.Naam, turnDateAround(gList.DatumAanmaak), gList.GelegenheidNaam, gList.PerNaam, gList.LijstCode}, gList)
                'grouping
                groupObjectInGroup(gList.GelegenheidNaam, flvi)
                flvGeschenkenLijsten.Items.Add(flvi)
            Else
                If gList.LijstActief = True Then
                    Dim flvi As New FilteredListViewItem({gList.Naam, turnDateAround(gList.DatumAanmaak), gList.GelegenheidNaam, gList.PerNaam, gList.LijstCode}, gList)
                    'grouping
                    groupObjectInGroup(gList.GelegenheidNaam, flvi)
                    flvGeschenkenLijsten.Items.Add(flvi)
                End If
            End If
        Next
        flvGeschenkenLijsten.FillListItems()
    End Sub

    Private Function isGroupObjectInGroup(ByRef name As String, ByRef groups As ListViewGroupCollection) As Boolean
        For Each groupObject As ListViewGroup In groups
            If groupObject.Header = name Then
                Return True
            End If
        Next
        Return False
    End Function

    Private Sub groupObjectInGroup(ByRef name As String, ByRef flvi As FilteredListViewItem)
        If isGroupObjectInGroup(name, flvGeschenkenLijsten.lstView.Groups) Then
            flvi.Group = flvGeschenkenLijsten.lstView.Groups(name)
        Else
            flvi.Group = flvGeschenkenLijsten.lstView.Groups.Add(name, name)
        End If
    End Sub





    Private Sub chkToonOudeLijsten_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkToonOudeLijsten.CheckedChanged
        FillItems()
    End Sub

    Private Sub btnNieuw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNieuw.Click
        Dim dialog As New frmGiftListWizard
        dialog.IsEdit = False
        Dim dateString As String = Today.Year.ToString & "/" & Today.Month.ToString("00") & "/" & Today.Day.ToString("00")
        dialog.GiftList = New GiftList(0, "", dateString, -1, -1, "", True, "", "")
        If (dialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            'save
            GiftList.AddGiftListToDb(dialog.GiftList, dialog.GiftListProductItems, frmMain.dbh)
            'get new list
            FillItems()
        End If
    End Sub

    Private Sub btnWijzigen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWijzigen.Click
        If Not flvGeschenkenLijsten.SelectedItem Is Nothing Then
            frmGiftListWizard.IsEdit = True
            frmGiftListWizard.GiftList = CType(flvGeschenkenLijsten.SelectedObject, GiftList)
            Dim giftListProductItems As List(Of GiftListProduct) = GiftListProduct.getItemsByGiftList(CType(flvGeschenkenLijsten.SelectedObject, GiftList), frmMain.dbh)
            frmGiftListWizard.GiftListProductItems = giftListProductItems
            If (frmGiftListWizard.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                'save
                GiftList.UpdateGiftList(frmGiftListWizard.GiftList, frmGiftListWizard.GiftListProductItems, frmMain.dbh)
                'get new list
                FillItems()
            End If
        Else
            MessageBox.Show("Geen geschenkenlijst geselecteerd!", "Geen selectie", MessageBoxButtons.OK, MessageBoxIcon.Hand)
        End If
    End Sub

    Private Sub btnVerwijderen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerwijderen.Click

        'look if someting is selected
        If Not flvGeschenkenLijsten.SelectedItem Is Nothing Then
            Dim giftL As GiftList = CType(flvGeschenkenLijsten.SelectedObject, GiftList)
            Dim giftlistItems As List(Of GiftListProduct) = GiftListProduct.getItemsByGiftList(giftL, frmMain.dbh)
            'look for orders
            For Each glp As GiftListProduct In giftlistItems
                If GiftListProduct.isItemBesteld(glp, frmMain.dbh) Then
                    MessageBox.Show("Er zijn reeds bestellingen van deze geschenkenlijst!", "Verwijderen onmogelijk", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                    Exit Sub
                End If
            Next
            'remove
            GiftList.removeGiftListFromDb(giftL, giftlistItems, frmMain.dbh)
            'update the list
            FillItems()
        Else
            MessageBox.Show("Geen geschenkenlijst geselecteerd!", "Geen selectie", MessageBoxButtons.OK, MessageBoxIcon.Hand)
        End If
    End Sub

    Private Sub btnDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetails.Click
        If Not flvGeschenkenLijsten.SelectedItem Is Nothing Then
            'roep de korte weergave van de wizard aan
            frmGiftListWizard.IsEdit = True
            frmGiftListWizard.GiftList = CType(flvGeschenkenLijsten.SelectedObject, GiftList)
            Dim giftListProductItems As List(Of GiftListProduct) = GiftListProduct.getItemsByGiftList(CType(flvGeschenkenLijsten.SelectedObject, GiftList), frmMain.dbh)
            frmGiftListWizard.GiftListProductItems = giftListProductItems
            frmGiftListWizard.showListDetails()
        Else
            MessageBox.Show("Geen geschenkenlijst geselecteerd!", "Geen selectie", MessageBoxButtons.OK, MessageBoxIcon.Hand)
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GroupedFillItems()
    End Sub


    Private Sub flvGeschenkenLijsten_TextChanged() Handles flvGeschenkenLijsten.FilterTextChanged
        flvGeschenkenLijsten.lstView.Groups.Clear() 'groups wegdoen
    End Sub

    Private Sub btnExporteren_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExporteren.Click
        If Not flvGeschenkenLijsten.SelectedItem Is Nothing Then
            sfd.InitialDirectory = Application.StartupPath
            sfd.Title = "Waar wilt u uw document opslaan?"

            If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim giftList As GiftList = CType(flvGeschenkenLijsten.SelectedObject, GiftList)
                Dim giftListProductItems As List(Of GiftListProduct) = GiftListProduct.getItemsByGiftList(CType(flvGeschenkenLijsten.SelectedObject, GiftList), frmMain.dbh)
                ExportToWord(sfd.FileName, giftList, giftListProductItems, My.Application.Info.DirectoryPath, frmMain.dbh)
                MessageBox.Show("Klaar")
            End If
            
        Else
            MessageBox.Show("Geen geschenkenlijst geselecteerd!", "Geen selectie", MessageBoxButtons.OK, MessageBoxIcon.Hand)
        End If
    End Sub

    

End Class